package com.zeta.demo.kt.common.config

import cn.dev33.satoken.stp.StpInterface
import com.zeta.api.client.auth.IAuthApi
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Component
import org.zetaframework.core.constants.RedisKeyConstants.USER_PERMISSION_KEY
import org.zetaframework.core.constants.RedisKeyConstants.USER_ROLE_KEY

/**
 * 权限数据源加载接口 实现类
 *
 * @author gcc
 */
@Component
class StpInterfaceImpl(private val authApi: IAuthApi): StpInterface {

    /**
     * 返回指定账号id所拥有的权限码集合
     *
     * @param loginId 账号id
     * @param loginType 账号类型
     * @return 该账号id具有的权限码集合
     */
    @Cacheable(value = [USER_PERMISSION_KEY], key = "#p0")
    override fun getPermissionList(
        loginId: Any?,
        loginType: String?
    ): List<String?>? {
        return authApi.permissions(loginId, loginType)
    }

    /**
     * 返回指定账号id所拥有的角色标识集合
     *
     * @param loginId 账号id
     * @param loginType 账号类型
     * @return 该账号id具有的角色标识集合
     */
    @Cacheable(value = [USER_ROLE_KEY], key = "#p0")
    override fun getRoleList(
        loginId: Any?,
        loginType: String?
    ): List<String?>? {
        return authApi.roles(loginId, loginType)
    }
}